<?php

namespace App\Models\Traits;

use Carbon\Carbon;
use App\Models\Product;

trait HasUserOrder
{
	public function hasValidOrder()
	{
		return $this->orders()->whereNotNull('paid_at')->exists();
	}
	
	public function hasNoValidOrder()
	{
		return !$this->hasValidOrder();
	}
	
	public function todayUnpaidOrdersCount()
	{
		return $this->orders()
			->whereDate('created_at', Carbon::today())
			->whereNull('paid_at')
			->count();
	}
	
	public function ordersCount()
	{
		$counts = [];
		$counts['unpaid'] = $this->orders()->unpaid()->count();
		$counts['undelivered'] = $this->orders()->undelivered()->count();
		$counts['delivered'] = $this->orders()->delivered()->count();
		$counts['uncommented'] = $this->orders()->uncommented()->count();
		return $counts;
	}

	public function addReward($uid,$productId)
	{
		$product = Product::find($productId);
		$balances = $product->price_vip;
        $data = ['product_id' => $productId];
        account_balances($this)->minusOrderPaid($balances, $data);
        $product->decrement('stock');
		$this->rewards()->create(['product_id'=>$productId,'is_open'=>'false','money'=>$product->price_vip]);

	}

	public function addActivity($activityId)
	{
		
		$this->activities()->create(['activity_id'=>$activityId]);

	}


}
